package to3;

import java.io.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.text.SimpleDateFormat;


public class betalingen extends javax.swing.JFrame {
    
    private KlantenLijst klantenlijst;

    public betalingen() {
        initComponents();
        setVisible(true);
        
                ////////SCHERM IN MIDDEN////////
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension windowSize = this.getSize();

        int windowX = Math.max(0, (screenSize.width  - windowSize.width ) / 2);
        int windowY = Math.max(0, (screenSize.height - windowSize.height) / 2);

        this.setLocation(windowX, windowY);
        klantenlijst = new KlantenLijst();
        try {
            readFile();
        } catch (IOException ex) {
            Logger.getLogger(betalingen.class.getName()).log(Level.SEVERE, null, ex);
        }
        fillKlantList();
    }
    
    
    @SuppressWarnings("unchecked")
    
    private void readFile() throws IOException {
                FileReader fr = new FileReader("klanten.txt");
                BufferedReader br = new BufferedReader(fr);

                    while (true) {
                    String regel = br.readLine();
                    if (regel == null) {
                    break;
                    }
                    Scanner sc = new Scanner(regel);
                    sc.useDelimiter("\\s*,\\s*");
                        String klantnr = sc.next();
                        String voornaam = sc.next();
                        String achternaam = sc.next();
                        String adres = sc.next();
                        String woonplaats = sc.next();
                        String telefoon = sc.next();
                        String email = sc.next();
                        String rekeningnr = sc.next();
                        klantenlijst.addKlant(klantnr, voornaam, achternaam, adres, woonplaats, telefoon, email, rekeningnr);
                        sc.close();
                    }
            // klaar met lezen uit file

                    br.close();
                    fr.close();
		
            // Uitlezen van de arraylist:
    }
            ///////////   
 
    private void fillKlantList(){
        List<Klant> tempList = klantenlijst.getKlanten();
            String s = "";
            for (Klant kl: tempList){
            s = s + kl.getKlantnr()+"      "+ kl.getVoornaam() + "      " + kl.getAchternaam() + ",";
            }
            final String[] sarray = stringtoArray( s, ",");
            jList2.setModel(new javax.swing.AbstractListModel() {
            String[] strings = sarray;
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
    }

  private String[] stringtoArray( String s, String sep ) {
  // convert a String s to an Array, the elements
  // are delimited by sep
  // NOTE : for old JDK only (<1.4).
  //        for JDK 1.4 +, use String.split() instead
  StringBuffer buf = new StringBuffer(s);
  int arraysize = 1;
  for ( int i = 0; i < buf.length(); i++ ) {
    if ( sep.indexOf(buf.charAt(i) ) != -1 )
      arraysize++;
  }
  String [] elements  = new String [arraysize];
  int y,z = 0;
  if ( buf.toString().indexOf(sep) != -1 ) {
    while (  buf.length() > 0 ) {
      if ( buf.toString().indexOf(sep) != -1 ) {
        y =  buf.toString().indexOf(sep);
        if ( y != buf.toString().lastIndexOf(sep) ) {
          elements[z] = buf.toString().substring(0, y ); 
          z++;
          buf.delete(0, y + 1);
        }
        else if ( buf.toString().lastIndexOf(sep) == y ) {
          elements[z] = buf.toString().substring
            (0, buf.toString().indexOf(sep));
          z++;
          buf.delete(0, buf.toString().indexOf(sep) + 1);
          elements[z] = buf.toString();z++;
          buf.delete(0, buf.length() );
        }
      }
    }
  }
  else {
    elements[0] = buf.toString(); 
  }
  buf = null;
  return elements;
}
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel1.setText("Betalingen");

        jLabel2.setText("Selecteer klant:");

        jScrollPane2.setViewportView(jList2);

        jButton1.setText("Toon factuur");
        jButton1.setActionCommand("Toon factuur");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Toon bedrag");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton4.setText("Hoofdmenu");
        jButton4.setPreferredSize(new java.awt.Dimension(164, 46));
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(jLabel2)
                .addGap(76, 76, 76)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2))
                .addContainerGap(404, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(348, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(323, 323, 323))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addGap(18, 18, 18)
                .addComponent(jButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 264, Short.MAX_VALUE)
                .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        String klantNr = jList2.getSelectedValue().toString().substring(0,8);
        facturen fac = new facturen(klantenlijst.getKlant(klantNr));               
        this.dispose();
}//GEN-LAST:event_jButton1ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        hoofdMenu hM = new hoofdMenu();
        this.dispose();
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        bedrag b = new bedrag();
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JList jList2;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables
}
